#pragma once
// -------------------------------------------------
// BlinKit - BlinKit Library
// -------------------------------------------------
//   File Name: web_page_host_window.h
// Description: WebPageHostWindow Class
//      Author: Ziming Li
//     Created: 2022-05-02
// -------------------------------------------------
// Copyright (C) 2022 MingYang Software Technology.
// -------------------------------------------------

#ifndef BLINKIT_WEB_PAGE_HOST_WINDOW_H
#define BLINKIT_WEB_PAGE_HOST_WINDOW_H

#include "blinkit/ui/web_page_host.h"
#include "blinkit/win/cursor_session.h"
#include "blinkit/win/mouse_session.h"
#include "blinkit/win/paint_session.h"

namespace BlinKit {

class Tooltip;

class WebPageHostWindow : public WebPageHost, public PaintSession::Client
{
public:
    ~WebPageHostWindow(void) override;

    operator HWND() const { return m_hWnd; }
protected:
    WebPageHostWindow(WebPage *page, HWND hWnd);

    void AttachHWND(HWND hWnd);
    bool HasHWND(void) const { return nullptr != m_hWnd; }
    void DetachHWND(void) { m_hWnd = nullptr; }

    void InitializePaintSession(HDC hdc, int cx, int cy) { m_paintSession.Initialize(hdc, cx, cy); }
    bool ProcessMessage(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam, LRESULT &result);

    void SetCapture(void) { m_mouseSession.SetCaptureIfPossible(m_hWnd); }
    void ReleaseCapture(void) { m_mouseSession.ReleaseCapture(); }
    void DisableMouseLeaveTracking(void) { m_mouseSession.DisableLeaveTracking(); }

    void OnShowWindow(HWND hwnd, BOOL fShow, UINT status);

#ifndef NDEBUG
    class MessageLogger {
    public:
        MessageLogger(const UINT msg);
        ~MessageLogger(void);
    private:
        const UINT m_msg;
    };
#endif

    // WebPageHost
    void HideTooltip(void) final;
private:
    void OnActivate(HWND hwnd, UINT state, HWND hwndActDeact, BOOL fMinimized);
    void OnMouse(UINT message, WPARAM wParam, LPARAM lParam);
    LRESULT OnNotify(HWND hwnd, int id, LPNMHDR nmhdr);
    void OnPaint(HWND hwnd);

    // AnimationProxy
    void FlushFrame(const SkBitmap &bitmap, const IntPoint &position, const IntSize &size, const SkPaint &paint) final;
    void SwapFrame(std::unique_ptr<AnimationFrame> &frame, const IntSize &size) final;
    // WebPageHost
    void UpdateCursor(const WebCursorInfo &cursorInfo) final;
    void ShowToolTip(const std::string &text, TextDirection dir, unsigned long cookie) final;
    // PaintSession::Client
    void BitBlt(HDC hMemoryDC, const IntRect &rect) final;

    HWND m_hWnd = nullptr;

    CursorSession m_cursorSession;
    MouseSession m_mouseSession;
    PaintSession m_paintSession;

    bool m_changingSizeOrPosition = false;

    std::unique_ptr<Tooltip> m_tooltip;
};

} // namespace BlinKit

#endif // BLINKIT_WEB_PAGE_HOST_WINDOW_H
